package org.jeecg.modules.app.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.api.announcement.entity.Announcement;
import org.jeecg.modules.api.announcement.service.IAnnouncementService;
import org.jeecg.modules.api.red.entity.RedCreate;
import org.jeecg.modules.api.red.entity.RedRecord;
import org.jeecg.modules.api.red.service.IRedCreateService;
import org.jeecg.modules.app.service.IIndexService;
import org.jeecg.modules.app.vo.IndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description: 红包接口
 * @author: Young Jun
 * @date: 2024年09月20日 9:57
 */
@Api(tags="红包接口")
@RestController
@RequestMapping("/app/red")
@Slf4j
public class RedController {

    @Resource
    private IRedCreateService redCreateService;
    @Autowired
    private IAnnouncementService announcementService;

    @PostMapping("create")
    @ApiOperation(value = "生成红包", notes = "生成红包")
    public Result<RedCreate> create(@RequestBody String req) {
        JSONObject jsonObject = JSONObject.parseObject(req);
        log.info("create:{}",req);
        String source = jsonObject.getString("source");
        RedCreate result = redCreateService.createRed(source);
        return Result.OK(result);
    }
    @ApiOperation(value = "领红包", notes = "领红包")
    @PostMapping("/getRed")
    public Result<RedCreate> getRed(@RequestBody String req) {
        JSONObject jsonObject = JSONObject.parseObject(req);
        log.info("getRed:{}",req);
        String redId = jsonObject.getString("redId");
        return Result.OK(redCreateService.getRed(redId));
    }

    @GetMapping("getRedByPage")
    @ApiOperation(value = "红包列表", notes = "红包列表")
    public Result<IPage<RedCreate>> getRedByPage( @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                      @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        IPage<RedCreate> pageList = redCreateService.getRedByPage(pageNo,pageSize);
        return Result.OK(pageList);
    }

    @GetMapping("getRedDetail")
    @ApiOperation(value = "红包详情", notes = "红包详情")
    public Result<RedCreate> getRedDetail( @RequestParam(name="redId") String redId) {
        log.info("getRedDetail:{}",redId);
        RedCreate result = redCreateService.getRedDetail(redId);
        return Result.OK(result);
    }

    @GetMapping("getJoinIng")
    @ApiOperation(value = "正在参与中的红包", notes = "红包")
    public Result<RedCreate> getJoinIng() {
        RedCreate result = redCreateService.getJoinIng();
        return Result.OK(result);
    }

//    @GetMapping("/getAnnouncementById")
//    public Result<Announcement> getAnnouncement(@RequestParam(name="id",required = false) String id) {
//        if (id != null) {
//            return Result.OK(announcementService.getById(id));
//        } else {
//            return Result.OK(announcementService.getOne(new LambdaQueryWrapper<Announcement>()
//                    .eq(Announcement::getIsDisplay, 1)
//                    .last("limit 1")));
//        }
//    }
}
